package alo;

import java.util.Scanner;

/**
 * @ClassName Main
 * @Description TODO
 * @Author Fanxc
 * @DATE 2021/4/16 18:53
 * @Version 1.0
 */
public class Main1 {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);

        int a=sc.nextInt();//5
        int b=sc.nextInt();//3
        int[] array=new int[a+1];
        int[] temp=new int[a+1];
        //初始化
        for (int i = 1; i <= a; i++) {
            array[i]=i;
        }
        //接收交换参数
        for (int i = 0; i < b; i++) {
            int m= sc.nextInt();
            int n= sc.nextInt();
            move(array,m,n);
        }

        int sum=isum(array,1,a,temp);
//        int sum=0;
//        for (int i = 1; i <=a ; i++) {
//            for (int j = i+1; j <=a ; j++) {
//                if (array[i]>array[j]){sum++;}
//            }
//        }
        System.out.println(sum);
    }

    private static int isum(int[] array,int low,int high,int[] temp) {
        if(low>=high)return 0;
        int m=(low+high)/2;
        int l=isum(array,low,m,temp);
        int r=isum(array,m+1,high,temp);
        int q=merge(array,low,high,temp);
        return l+r+q;
    }

    private static int merge(int[] array, int low, int high, int[] temp) {
        int m=(low+high)/2;
        int start=low;
        int l=low;
        int h=m+1;
        int sum=0;
        for (int i = low; i <=high; i++) {
            temp[i]=array[i];
        }
        while (l<=m&&h<=high){
            if (temp[l]<temp[h]){
                array[start++]=temp[l];
                l++;
            }else {
                array[start++]=temp[h];
                h++;
                sum+=m-l+1;
            }
        }
        while (l<=m){
            array[start++]=temp[l];
            l++;
        }
        while (h<=high){
            array[start++]=temp[h++];
        }

        return sum;
    }

    private static void move(int[] array, int m, int n) {
        int s=n-m+1;
        int[] a=new int[s];
        for (int i = 0; i < s; i++) {
            a[i]=array[m++];
        }
        for (int i = 0; i < s; i++) {
            array[n--]=a[i];
        }
    }
}
